package com.lge.clock.worldclock.cities;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Rect;
import android.view.MotionEvent;

/**
 * @author lge.com
 */
public class CityInfoDisplay {
    private ArrayList<CityInformation> mCityInformationList;
    private ArrayList<CityInformation> mDisplayCityInfoList;
    
    protected ArrayList<CityInformation> mSelectedCityNameList;
    
    private int mCityCount = 0;
    private CityDBManager mCityManager;

    public CityInfoDisplay(Context context) {
    	try {
    		mCityManager = CityDBManager.getInstance(context);
    		mCityInformationList = mCityManager.getCityInfoMapList();
    		mCityCount = mCityInformationList.size();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}

        initSetting();
    }

    protected void initSetting() {
        initDisplayCityInfoList();

        initSelectedCityNameList();
    }

    public CityInformation getCityInformation(int index) {
    	if(index >= mCityInformationList.size()) {
    		mCityInformationList = mCityManager.getCityInfoMapList();
            mCityCount = mCityInformationList.size();
    	}
        return mCityInformationList.get(index);
    }

    public ArrayList<CityInformation> getCityInformationList() {
        return mCityInformationList;
    }
    
    public int getCityCount() {
    	if(0 == mCityInformationList.size()) {
    		mCityInformationList = mCityManager.getCityInfoMapList();
            mCityCount = mCityInformationList.size();
    	}
        return mCityCount;
    }

    public void initDisplayCityInfoList() {
        if (mDisplayCityInfoList == null) {
            mDisplayCityInfoList = new ArrayList<CityInformation>();
        }

        mDisplayCityInfoList.clear();
    }

    public void initSelectedCityNameList() {
        if (mSelectedCityNameList == null) {
            mSelectedCityNameList = new ArrayList<CityInformation>();
        }

        mSelectedCityNameList.clear();
    }

    public void addDisplayCityInfo(CityInformation cityInfo) {
        mDisplayCityInfoList.add(cityInfo);
    }

    @SuppressWarnings("unchecked")
    public boolean isSelectedCity(MotionEvent event) {
        initSelectedCityNameList();

        boolean isSelected = false;
        float x = event.getX();
        float y = event.getY();
        
        ArrayList<CityInformation> cloneList = (ArrayList<CityInformation>) mDisplayCityInfoList.clone();

        for(CityInformation cityInfo : cloneList) {
            int aroundLength = 20;

            float cityLocationX = cityInfo.getWindowX();
            float cityLocationY = cityInfo.getWindowY();
            Rect cityLocationRect = cityInfo.getWindowRect();

            if(cityLocationRect.contains((int)x, (int)y)) {
            	mSelectedCityNameList.add(cityInfo);
            	isSelected = true;
            }
            else if (x - aroundLength <=  cityLocationX && x + aroundLength >= cityLocationX &&
                    y - aroundLength <= cityLocationY && y + aroundLength >= cityLocationY) {
                mSelectedCityNameList.add(cityInfo);
                isSelected = true;
            }
        }

        return isSelected;
    }

    public ArrayList<CityInformation> getSelectedCityInfoList() {
        return mSelectedCityNameList;
    }
}
